<template>
  <div class="td-status">
    <div class="header">
      <img src="../assets/images/gril.png">
      <p>{{user.UserName}}</p>
      <p>{{user.MobilePhone}}</p>
    </div>

    <el-row>
      <div class="td-form-item">
        <el-col :span="24">
          <el-input
            type="password"
            placeholder="请输入密码"
            v-model="params.Password"
            style="width:100%;"
            prefix-icon="icon font_family icon-td-mima"
          ></el-input>
        </el-col>
      </div>
      <div class="td-form-item">
        <el-col :span="17">
          <el-input
            placeholder="请输入验证码"
            v-model="params.ValidateCode"
            style="width:100%;"
            prefix-icon="icon font_family icon-td-youxiang"
          ></el-input>
        </el-col>
        <el-col :span="7">
          <img width="100%" height="40" v-bind:src="ImgByteArray" @click="loadValidateCode();">
        </el-col>
      </div>
      <div class="td-form-item">
        <el-col :span="24" style="text-align:center;">
          <el-button
            type="primary" v-if="type===1"
            class="td-btn-patient-primary"
            style="width:80%;"
            round
            @click="btnSubmit();"
          >登录</el-button>


          <el-button
            type="primary" v-if="type===0"
            class="td-btn-doctor-primary"
            style="width:80%;"
            round
            @click="btnSubmit();"
          >登录</el-button>
        </el-col>
      </div>
    </el-row>
  </div>
</template>

<script>
import AuthService from "../_services/auth.service";
import UserService from "../_services/user.service";
import DoctorService from "../_services/doctor.service";
import HospitalService from "../_services/hospital.service";
import wxService from "../_services/wx.service";
export default {
  name: "TianDingStatusComponent",
  data() {
    return {
      params: {
        Mobile: "",
        Password: "",
        ValidateCode: "",
        OpenID: "",
        Access_Token: ""
      },
      ImgByteArray: "",
      ValidateCode: "",
      user: {},
      type: 0
    };
  },
  mounted() {
    this.user = this.local$.getUser();
    this.type = this.local$.getType();
    this.loadValidateCode();
  },
  methods: {
    loadValidateCode() {
      AuthService.getValidateCode().then(response => {
        if (response !== null) {
          let { Data } = response;
          this.ValidateCode = Data.ValidateCode;
          this.ImgByteArray = `data:image/jpeg;base64,${Data.ImgByteArray}`;
        }
      });
    },
    async btnSubmit() {
      this.params.Mobile = this.user.MobilePhone;

      if (!this.params.Password) {
        this.toast$("请输入密码!");
        return;
      }

      if (
        this.encry$.MD5Encryption(this.params.ValidateCode) !==
        this.ValidateCode
      ) {
        this.toast$("请输入正确的验证码!");
        return;
      }

      this.indicator$.open("请稍后...");

      let wxParams = {};
      wxParams.code = this.$route.query.code;

      let accessToken = await wxService.getOpenId(wxParams);

      this.params.OpenID = accessToken.Data.Openid;

      this.params.Access_Token = accessToken.Data.Access_Token;

      if (this.type === 0) {
        DoctorService.loginDoctor(this.params).then(response => {
          let { Data } = response;
          if (!this.utils$.isEmpty(Data.IconPath)) {
            Data.IconPath = require("../assets/images/boy.png");
          }

          this.utils$.setToken(Data.Password);

          let params = {};
          params.doctorID = Data.DoctorID;
          HospitalService.getHospitalListByDoctor(params).then(
            hospitalResponse => {
              Data.hospitalList = hospitalResponse.Data;

              this.utils$.setUser(Data);
              this.utils$.setStatus(true);
              this.utils$.setCookie();

              this.toast$("登陆成功");
              this.$router.push(`/tdDoctor`);
              this.indicator$.close();
            }
          );
        });
      } else {
        UserService.loginTD(this.params).then(response => {
          let { Data } = response;
          if (!this.utils$.isEmpty(Data.IconPath)) {
            Data.IconPath = require("../assets/images/gril.png");
          }

          this.utils$.setToken(Data.Password);
          this.utils$.setUser(Data);
          this.utils$.setStatus(true);
          this.utils$.setCookie();

          this.toast$("登陆成功");
          this.$router.push(`/tdPatient`);
          this.indicator$.close();
        });
      }
    }
  }
};
</script>

<style lang="scss">
</style>


